package com.btkjgc.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.btkjgc.controller.BaseController;
import com.btkjgc.models.Dictionary;
import com.btkjgc.models.Role;
import com.btkjgc.service.intf.RoleManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class RoleManageService_Impl extends BaseService_Impl implements RoleManageService_Intf{
	
	public BaseController baseController;
	Logger logger = Logger.getLogger(RoleManageService_Impl.class);

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getRoleList() 
	{
		String hql = "select DISTINCT r from Role r where r.del=false and r.level<="+getLoginUser().getRoleLevel()+" order by r.id desc";
		
		List<Role> roleList = HibernateUtil.executeQueryList(hql, null);
		
		List<Role> list = new ArrayList<Role>();
		for(Role role : roleList){
			list.add(role);
		}
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dictionary> getLevelList() 
	{
		String hql = "select DISTINCT Dictionary from Dictionary Dictionary where Dictionary.name='Level' order by Dictionary.key";
		
		List<Dictionary> levelList = HibernateUtil.executeQueryList(hql, null);
		
		List<Dictionary> list = new ArrayList<Dictionary>();
		for(Dictionary dic : levelList){
			list.add(dic);
		}
		
		return list;
	}

	@Override
	public void insertRole(HttpServletRequest request, String name,
			Integer level) {
		String hql="select max(id) from Role";
		int id = HibernateUtil.executeQueryCount(hql, null)+1;
		// role中插入一条新的记录
		Role role = new Role();
		role.setId(id);
		role.setName(name);
		role.setLevel(1);
		role.setStatus(0);
		role.setDel(false);
		HibernateUtil.save(role);
	}

	@Override
	public void updateRole(String name, String level, String id) {
		String[] param = null;
		String hql="update Role set name=? where id=?";
		param = new String[] { name, id };
		
		HibernateUtil.executeUpdate(hql, param);
	}

	@Override
	public void deleteById(String id) {
		String hql="update Role set del=1 where id=?";
		
		HibernateUtil.executeUpdate(hql, new String[] { id });
	}

	@Override
	public void batchDel(String[] id) {
		String hql="update Role set del=1 where id=?";
		
		for(int i=0;i<id.length;i++){
			HibernateUtil.executeUpdate(hql, new String[] { id[i] });
		}
	}
	
}